package com.yang.job.infra.event.configuration;

import com.yang.job.infra.event.EventBusContext;
import com.yang.job.infra.event.EventCenter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@Configuration
public class EventBusConfiguration {
    @Bean
    public ExecutorService asyncEventExecutorService() {
        return new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
                Runtime.getRuntime().availableProcessors() * 2 + 1,
                60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(200),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }

    @Bean
    public EventCenter eventCenter() {
        return new EventCenter(asyncEventExecutorService());
    }

    @Bean
    public EventBusContext eventBusContext() {
        return new EventBusContext();
    }
}

